package at.ptasat.solver;

import java.io.File;
import java.io.OutputStream;

import at.ptasat.util.SequenceInsertingOutputStream;

public abstract class AsyncSolver {
	private static final byte[] PREFIX = "\n".getBytes();
	
	private final int id;
	private final AsyncSolverListener callback;
	private final OutputStream out, err;
	private final String argument;

	public AsyncSolver(int id, AsyncSolverListener callback, OutputStream out, OutputStream err, String argument) {
		this.id = id;
		this.callback = callback;
		final byte[] sequence = String.format(" (worker %d)", id).getBytes();
		this.out = new SequenceInsertingOutputStream(out, PREFIX, sequence);
		this.err = new SequenceInsertingOutputStream(err, PREFIX, sequence);
		this.argument = argument;
		callback.enroll(this);
	}

	public int getId() {
		return id;
	}

	public String getArguments() {
		return argument;
	}

	public OutputStream getOut() {
		return out;
	}

	public OutputStream getErr() {
		return err;
	}

	protected void fireSolved() {
		callback.solved(this);
	}

	protected void fireFailed() {
		callback.failed(this);
	}

	public abstract void start(String additionalArgument, File file) throws Exception;

	public abstract void kill();

}
